export const strToTime = (timeStr: string, format = "yyyy-MM-dd") => {
    const parse = Date.parse(timeStr);
    if (!isNaN(parse)) {
        return timeFormat(parse)
    } else {
        return "未知时间"
    }
}

export const timeFormat = (timestamp: number | string = new Date().getTime(), format: string = "yyyy-MM-dd hh:mm:ss") => {
    //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let date;
    if (typeof timestamp === 'string') {
        date = new Date(parseInt(timestamp) * 1000);
    } else {
        date = new Date(timestamp);
    }

    let o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(), // day
        "h+": date.getHours(), // hour
        "m+": date.getMinutes(), // minute
        "s+": date.getSeconds(), // second
        "q+": Math.floor((date.getMonth() + 3) / 3), // quarter
        "S": date.getMilliseconds()
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }

    for ( let k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            // @ts-ignore
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}